package com.atguigu.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.atguigu.realtime.app.BaseAppV2;
import com.atguigu.realtime.bean.OrderWide;
import com.atguigu.realtime.bean.PaymentInfo;
import com.atguigu.realtime.bean.PaymentWide;
import com.atguigu.realtime.util.FlinkSinkUtil;
import com.atguigu.realtime.util.KKutil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.HashMap;

import static com.atguigu.realtime.common.Constant.*;


public class DwmPaymentWideApp extends BaseAppV2 {

    public static void main(String[] args) {

        new DwmPaymentWideApp().init(3004,1,"DwmPaymentWideApp","DwmPaymentWideApp",
                                    TOPIC_DWD_PAYMENT_INFO,TOPIC_DWM_ORDER_WIDE);
    }
    @Override
    protected void run(StreamExecutionEnvironment env,
                       HashMap<String, DataStreamSource<String>> topicToStream) {

       /* topicToStream.get(TOPIC_DWD_PAYMENT_INFO).print("2"); 目前只输出2 可能太卡了
        topicToStream.get(TOPIC_DWM_ORDER_WIDE).print("1");*/

        KeyedStream<PaymentInfo,Long> paymentInfoStream = topicToStream
                .get(TOPIC_DWD_PAYMENT_INFO)
                .map(json -> JSON.parseObject(json, PaymentInfo.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((info, ts) -> KKutil.toTs(info.getCreate_time()))
                )
                .keyBy(PaymentInfo::getOrder_id);

        KeyedStream<OrderWide,Long> orderWideStream = topicToStream
                .get(TOPIC_DWM_ORDER_WIDE)
                .map(json -> JSON.parseObject(json, OrderWide.class))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((ow, ts) -> KKutil.toTs(ow.getCreate_time()))
                )
                .keyBy(OrderWide::getOrder_id);
            //双流join
        paymentInfoStream
                .intervalJoin(orderWideStream)
                .between(Time.minutes(-45),Time.seconds(10))
                .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                    @Override
                    public void processElement(PaymentInfo left,
                                               OrderWide right,
                                               Context ctx,
                                               Collector<PaymentWide> out) throws Exception {
                        out.collect(new PaymentWide(left,right));
                    }
                })
                //.print();
                .map(JSON::toJSONString)
                .addSink(FlinkSinkUtil.getKafkaSink(TOPIC_DWM_PAYMENT_WIDE));
    }
}
